<?php

/**
 * This is the model class for table "lths_product".
 *
 * The followings are the available columns in table 'lths_product':
 * @property string $product_id
 * @property string $product_name
 * @property string $specification
 * @property integer $producttype_code
 * @property string $create_time
 * @property string $update_time
 * @property double $procurement_price
 * @property string $ram_rom
 * @property string $screen_size
 * @property string $os
 * @property string $supplier
 * @property string $brand_code
 * @property string $parameters
 * @property string $fit_phone
 *
 * The followings are the available model relations:
 * @property Dayreport[] $dayreports
 * @property Lend[] $lends
 * @property Producttype $producttypeCode
 * @property Producttype[] $lthsProducttypes
 * @property Profit[] $profits
 * @property Purchase[] $purchases
 * @property TradeGeneral[] $tradeGenerals
 * @property TradeTreaty[] $tradeTreaties
 */
class Product extends CActiveRecord
{
	public $keyname='product_id';
	public $displayname='product_name';
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Product the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'lths_product';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('producttype_code', 'numerical', 'integerOnly'=>true),
			array('procurement_price', 'numerical'),
			array('product_id', 'length', 'max'=>15),
			array('product_name, ram_rom, screen_size, os, supplier, brand_code, parameters, fit_phone', 'length', 'max'=>50),
			array('specification', 'length', 'max'=>100),
			array('create_time, update_time', 'safe'),
			array('product_id, product_name, specification, producttype_code', 'required'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('product_id, product_name, specification, producttype_code, create_time, update_time, procurement_price, ram_rom, screen_size, os, supplier, brand_code, parameters, fit_phone', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'dayreports' => array(self::HAS_MANY, 'Dayreport', 'product_id'),
			'lends' => array(self::HAS_MANY, 'Lend', 'product_id'),
			'producttype' => array(self::BELONGS_TO, 'Producttype', 'producttype_code'),
			'producttypeCode' => array(self::MANY_MANY, 'Producttype', 'lths_producttype_product(product_id, producttype_code)'),
			'profits' => array(self::HAS_MANY, 'Profit', 'product_id'),
			'purchases' => array(self::HAS_MANY, 'Purchase', 'product_id'),
			'tradeGenerals' => array(self::HAS_MANY, 'TradeGeneral', 'product_id'),
			'tradeTreaties' => array(self::HAS_MANY, 'TradeTreaty', 'product_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'product_id' => '商品编码',
			'product_name' => '商品名称',
			'specification' => '规格说明',
			'producttype_code' => '商品类型',
			'create_time' => 'Create Time',
			'update_time' => 'Update Time',
			'procurement_price' => '采购价',
			'ram_rom' => 'RAM/ROM',
			'screen_size' => '屏幕大小(寸)',
			'os' => '操作系统',
			'supplier' => '供应商',
			'brand_code' => '品牌',
			'parameters' => '配件基本参数',
			'fit_phone' => '适配机型',
		);
	}
	
	function getListDataByType($type)
	{
		$results = array();
		if(isset($type))
		{
			$models = $this->findAllByAttributes(array('producttype_code'=>$type));
		    foreach ($models as $key => $value) {
		    	$results[$value[$this->keyname]] = $value[$this->displayname];
		    }
		}
		
	    return $results;
	}
	
	
	function getTypeJson()
	{
		$results = array();
		$producttypeModels = Producttype::model()->findAll();
		foreach($producttypeModels as $producttype)
		{
			$results[$producttype->producttype_code] = array();
			foreach($producttype->products as $product)
			{
				$results[$producttype->producttype_code][$product->product_id] = $product->product_name;
			}
		}
		return CJSON::encode($results);
	}
	
	
	public function getProducttypeJson()
	{
		$results = array();
		$producttypeModels = Producttype::model()->findAll();
		foreach($producttypeModels as $producttype)
		{
			$results[$producttype->producttype_code] = array();
			foreach($producttype->products as $product)
			{
				$results[$producttype->producttype_code][$product->product_id] = $product->product_name;
			}
		}
		return CJSON::encode($results);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('product_id',$this->product_id,true);
		$criteria->compare('product_name',$this->product_name,true);
		$criteria->compare('specification',$this->specification,true);
		$criteria->compare('producttype_code',$this->producttype_code);
		$criteria->compare('create_time',$this->create_time,true);
		$criteria->compare('update_time',$this->update_time,true);
		$criteria->compare('procurement_price',$this->procurement_price);
		$criteria->compare('ram_rom',$this->ram_rom,true);
		$criteria->compare('screen_size',$this->screen_size,true);
		$criteria->compare('os',$this->os,true);
		$criteria->compare('supplier',$this->supplier,true);
		$criteria->compare('brand_code',$this->brand_code,true);
		$criteria->compare('parameters',$this->parameters,true);
		$criteria->compare('fit_phone',$this->fit_phone,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	public function afterSave()
	{
		//Yii::log('onAfterSave User','Product');
		$ProducttypeProduct = new ProducttypeProduct;
		$ProducttypeProduct->deleteAll("producttype_code=4 and product_id='".$this->product_id."'");
		$ProducttypeProduct->producttype_code=$this->producttype_code;
		$ProducttypeProduct->product_id=$this->product_id;
		$ProducttypeProduct->save();
	}
}